// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   EBook.java

package com.kaylyn.item;

import com.kaylyn.util.Resource;
import java.awt.Dimension;
import java.awt.Point;
import java.io.IOException;
import java.util.HashMap;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

// Referenced classes of package com.kaylyn.item:
//            Page

public class EBook
{
	private static EBook instance;

    public EBook(Dimension offscreenDimension, Point offscreenLocation)
    {
    	instance = this;
        pages = new HashMap();
        try
        {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setIgnoringComments(true);
            factory.setCoalescing(true);
            factory.setNamespaceAware(false);
            factory.setValidating(false);
            DocumentBuilder parser = factory.newDocumentBuilder();
            Document document = parser.parse(Resource.loadFile("book.xml"));
            NodeList pageList = document.getElementsByTagName("page");
            Page temp_1 = null;
            Page temp_2 = null;
            for(int i = 0; i < pageList.getLength(); i++)
            {
                NodeList pageElement = pageList.item(i).getChildNodes();
                temp_1 = new Page(this, offscreenLocation, offscreenDimension);
                for(int j = 0; j < pageElement.getLength(); j++)
                {
                    String nodeName = pageElement.item(j).getNodeName();
                    if(nodeName.equals("name"))
                    {
                        String name = pageElement.item(j).getFirstChild().getNodeValue();
                        if(!pages.containsKey(name))
                            pages.put(name, temp_1);
                    }
                    else if(nodeName.equals("description")){
                    	Node child = pageElement.item(j).getFirstChild();
                    	if(child != null)
                    		temp_1.setDescription(child.getNodeValue());
                    }
                    else if(nodeName.equals("img"))
                        temp_1.addImage(pageElement.item(j));
                    else if(nodeName.equals("applet"))
                        temp_1.addComponent(pageElement.item(j));
                    else if(nodeName.equals("button"))
                        temp_1.addButton(pageElement.item(j));
                }

                if(temp_2 != null)
                {
                    temp_2.setNextPage(temp_1);
                    temp_1.setLastPage(temp_2);
                }
                if(currentPage == null)
                    currentPage = temp_1;
                temp_2 = temp_1;
            }

        }
        catch(ParserConfigurationException e)
        {
            e.printStackTrace();
        }
        catch(SAXException e)
        {
            e.printStackTrace();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }

    public static EBook getInstance(){
    	return instance;
    }
    
    public Page getCurrentPage()
    {
        return currentPage;
    }

    public Page setCurrentPage(Page page)
    {
        currentPage = page;
        return currentPage;
    }

    public Page getPage(String name)
    {
        return (Page)pages.get(name);
    }

    public boolean hasNextPage()
    {
        return currentPage.getNextPage() != null;
    }

    public Page getNextPage()
    {
        return currentPage.getNextPage();
    }

    public boolean hasLastPage()
    {
        return currentPage.getLastPage() != null;
    }

    public Page getLastPage()
    {
        return currentPage.getLastPage();
    }

    public Page getCoverPager()
    {
        return (Page)pages.get("cover");
    }

    public Page getBackCoverPage()
    {
        return (Page)pages.get("backCover");
    }

    public Page getContentPage()
    {
        return (Page)pages.get("content");
    }

    private Page currentPage;
    private HashMap pages;
}
